//
//  FanKui.m
//  PCAForWar
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FanKui.h"
#import <DKNightVersion.h>

#define WIDTH    [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHT   [UIScreen mainScreen].bounds.size.height / 667
@interface FanKui () <UITextViewDelegate>

@property (nonatomic, retain) UIView *viewForidea;
@property (nonatomic, retain) UILabel *placeholderLabel;
@property (nonatomic, retain) UITextView *textForIdea;
@property (nonatomic, retain) UIButton *buttonForSend;

@end

@implementation FanKui

- (void)dealloc{
    
    _textForIdea.delegate = nil;
    
    [_viewForidea release];
    [_placeholderLabel release];
    [_textForIdea release];
    [_buttonForSend release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor clearColor];
    self.navigationItem.title = @"意见反馈";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter-Bold" size:22],}];
    
    [self createView];
    [self createTextFiled];
    [self createButton];
}

#pragma mark 夜间模式
- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.viewForidea.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    self.textForIdea.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
}

#pragma mark 创建搜索界面
- (void)createView {
    
    self.viewForidea = [[UIView alloc] initWithFrame:self.view.frame];
    
    self.viewForidea.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.viewForidea];
    
    [_viewForidea release];
}

#pragma mark TextFiled
- (void)createTextFiled {
    
    self.textForIdea = [[UITextView alloc] initWithFrame:CGRectMake(20 * WIDTH, 100 * HEIGHT, 340 * WIDTH, 200 * HEIGHT)];
    
    self.textForIdea.backgroundColor = [UIColor whiteColor];
    
    [self.viewForidea addSubview:self.textForIdea];
    
    self.textForIdea.tag = 100;
    
    /** 占位符 */
    self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.textForIdea.frame), 20)];
    self.placeholderLabel.backgroundColor = [UIColor clearColor];
    self.placeholderLabel.text = @"您有什么意见或者建议,说几句吧!";
    self.placeholderLabel.font = self.textForIdea.font;
    [self.textForIdea addSubview:self.placeholderLabel];
    
    /** 设置光标在起始位置 */
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    /** 边框 */
    self.textForIdea.layer.cornerRadius = 6.0f;
    self.textForIdea.layer.borderWidth = 2;
    self.textForIdea.layer.borderColor = [[UIColor colorWithRed:200.0/255 green:50/255 blue:10/255 alpha:1]CGColor];
    
    /** 字体 */
    self.textForIdea.font=[UIFont fontWithName:@"Arial" size:18.0];
    
    /** 是否允许编辑内容*/
    self.textForIdea.scrollEnabled = NO;
    
    /** 弹出键盘的类型 */
    self.textForIdea.keyboardType = UIKeyboardTypeDefault;
    
    /** 弹出键盘的风格(亮/暗) */
    self.textForIdea.keyboardAppearance = UIKeyboardAppearanceDark;
    
    /** 键盘回车按键 */
    self.textForIdea.returnKeyType = UIReturnKeyJoin;
    
    self.textForIdea.delegate = self;
    
    [_textForIdea release];
    [_placeholderLabel release];
    
}

#pragma mark textfield协议方法
/** 将要开始编辑的时候调用此协议方法 */
- (BOOL)textViewShouldBeginEditing:(UITextField *)textField{
    
    return YES;
}

#pragma mark 模仿textfield占位符
- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    self.placeholderLabel.text = @"";
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    if (self.textForIdea.text.length == 0) {
        
        self.placeholderLabel.text = @"您有什么意见或者建议,说几句吧!";
        
    } else {
        
        self.placeholderLabel.text = @"";
    }
    
}

/** 点击键盘上的return按键时,调用此方法 */
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        
        return NO;
    }
    
    return YES;
}

#pragma mark Button
- (void)createButton {
    
    self.buttonForSend = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.buttonForSend.frame = CGRectMake(20 * WIDTH, 320 * HEIGHT, 340 * WIDTH, 50 * HEIGHT);
    
    self.buttonForSend.backgroundColor = [UIColor redColor];
    
    [self.buttonForSend.layer setMasksToBounds:YES];
    [self.buttonForSend.layer setCornerRadius:10.0];
    
    [self.viewForidea addSubview:self.buttonForSend];
    
    [self.buttonForSend setTitle:@"发送" forState:UIControlStateNormal];
    
    self.buttonForSend.titleLabel.font = [UIFont systemFontOfSize: 18.0];
    
    [self.buttonForSend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.buttonForSend addTarget:self action:@selector(SendAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)SendAction:(UIButton *)button {
    
    [self.textForIdea resignFirstResponder];
    
    if (self.textForIdea.text.length != 0) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *actionDelete = [UIAlertAction actionWithTitle:@"Ok" style:1 handler:^(UIAlertAction * _Nonnull action) {
            
            self.textForIdea.text = nil;
            
            self.placeholderLabel.text = @"您有什么意见或者建议,说几句吧!";
            
        }];
        
        [alert addAction:actionDelete];
        
        /** 模态推出 */
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    } else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"您输入的反馈不能为空或者有特殊字符" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *actionDelete = [UIAlertAction actionWithTitle:@"Ok" style:1 handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:actionDelete];
        
        /** 模态推出 */
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
